package com.yunkaweilai.android.activity.operation.consumption.guadan;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.MainActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.fragment.guadan.GuadanFragment;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.utils.TitleBuilder;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 挂单列表
 * Created by Administrator on 2017/8/9.
 */

public class GuadanListActivity extends BaseActivity {

    @BindView(R.id.id_viewPager)
    ViewPager idViewPager;
    @BindView(R.id.titlebar_iv_left)
    ImageView titlebarIvLeft;
    @BindView(R.id.titlebar_tv_left)
    TextView titlebarTvLeft;
    @BindView(R.id.titlebar_tv)
    TextView titlebarTv;
    @BindView(R.id.titlebar_iv_right_2)
    ImageView titlebarIvRight2;
    @BindView(R.id.titlebar_iv_right)
    ImageView titlebarIvRight;
    @BindView(R.id.titlebar_tv_right)
    TextView titlebarTvRight;
    @BindView(R.id.id_view_underline)
    View idViewUnderline;
    @BindView(R.id.rl_titlebar)
    RelativeLayout rlTitlebar;
    @BindView(R.id.id_rbtn_1)
    RadioButton idRbtn1;
    @BindView(R.id.id_rbtn_2)
    RadioButton idRbtn2;
    @BindView(R.id.id_group)
    RadioGroup idGroup;
    @BindView(R.id.id_view1)
    View idView1;
    @BindView(R.id.id_view2)
    View idView2;
    private String[] titls = new String[]{"正在挂单", "历史挂单"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_order1);
        ButterKnife.bind(this);
        new TitleBuilder(mAc).setTitleText("挂单列表")
                .setLeftImage(R.mipmap.ic_go_back)
                .setLeftOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        intent2Activity(MainActivity.class);
                    }
                });
        GuadanTypeAdapter adapter = new GuadanTypeAdapter(getSupportFragmentManager());
        idViewPager.setAdapter(adapter);
        initClick();
        if (!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void onTitleNumEvent(Event.NumTypeEvent event){
        if (event!=null){
            if (event.type == CommonConstants.GUADAN_TITLE_NUM) {
                if (event.flag){
                    idRbtn1.setText("正在挂单(" + event.nums +")");
                }else{
                    idRbtn2.setText("历史挂单(" + event.nums +")");
                }
            }
        }
    }


    private void initClick() {
        idGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.id_rbtn_1:
                        idView1.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                        idView2.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                        idViewPager.setCurrentItem(0,true);
                        break;
                    case R.id.id_rbtn_2:
                        idViewPager.setCurrentItem(1,true);
                        idView2.setBackgroundColor(getResources().getColor(R.color.wjx_title_bg_next));
                        idView1.setBackgroundColor(getResources().getColor(R.color.bottom_rgb_bule_false));
                        break;
                }
            }
        });

        idViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        idGroup.check(R.id.id_rbtn_1);
                        break;
                    case 1:
                        idGroup.check(R.id.id_rbtn_2);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    class GuadanTypeAdapter extends FragmentStatePagerAdapter {

        public GuadanTypeAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            if (position == 0) {
                fragment = GuadanFragment.getInstence("0", "1");
            } else if (position == 1) {
                fragment = GuadanFragment.getInstence("1", "0");
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return titls.length;
        }

    }
}
